package com.cheng.im.gateway.handler;

import com.cheng.im.common.constants.ServerConstants;
import com.cheng.im.gateway.session.manager.SessionManger;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@ChannelHandler.Sharable
@Service("ServerExceptionHandler")
public class ServerExceptionHandler extends ChannelInboundHandlerAdapter
{
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
    {
//        if (cause instanceof InvalidFrameException)
//        {
//            log.error(cause.getMessage());
//        } else
//        {
//            //捕捉异常信息
//            cause.printStackTrace();
//            log.error(cause.getMessage());
//        }

        //捕捉异常信息
        cause.printStackTrace();
        log.error(cause.getMessage());

        SessionManger.inst().closeSession(ctx);
        ctx.close();
    }

    /**
     * 通道 Read 读取 Complete 完成
     * 做刷新操作 ctx.flush()
     */
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
    {
        ctx.flush();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx)
            throws Exception
    {
        log.info("客户端断开连接：{}", ctx.channel().attr(ServerConstants.CHANNEL_NAME).get());
        SessionManger.inst().closeSession(ctx);
    }


}